<template>
  <div class="container">
    <el-card shadow="never">
      <el-form ref="form" :model="query" label-width="80px">
        <el-row>
          <!-- 搜索框 -->
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input placeholder="按编号搜索" v-model="query.keyword"></el-input>
            </el-form-item>
          </el-col>
          <!-- 按钮 -->
          <el-col :span="18" style="text-align:right;">
            <el-button size="small" @click="clear">清除</el-button>
            <el-button size="small" type="primary" @click="onSearch()">搜索</el-button>
          </el-col>
        </el-row>
      </el-form>
      <!-- 警示框 -->
      <el-alert :title="cTitle" type="info" :closable="false" show-icon></el-alert>
      <!-- 表单 -->
      <template>
        <el-table :data="data" style="width: 100%">
          <el-table-column prop="id" label="编号" width="200"> </el-table-column>
          <el-table-column prop="questionType" label="题型" width="100">
            <template slot-scope="scope">
              <p>{{scope.row.questionType | fMapQuestionType}}</p>
            </template>
          </el-table-column>
          <el-table-column prop="questionIDs" label="题目编号" width="200">
            <template slot-scope="scope">
              <div v-for="(item, index) in scope.row.questionIDs" :key="index">
                <p class="number" @click="preview(item.id)">{{ item.number }}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="addTime" label="录入时间" width="180"> </el-table-column>
          <el-table-column prop="totalSeconds" label="答题时间" width="100"> </el-table-column>
          <el-table-column prop="accuracyRate" label="正确率" width="180"> </el-table-column>
          <el-table-column prop="userName" label="录入人" width="180"> </el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button type="danger" icon="el-icon-delete" plain circle @click="remove(scope.row)"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页功能 -->
      <el-pagination style="text-align: right; margin-top: 20px; margin-bottom: 15px;" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="query.page" :page-sizes="[ 20, 30, 50,100]" :page-size="query.pagesize" layout="prev, pager, next, sizes, jumper" background :total="count">
      </el-pagination>
      <!-- 分页功能 end-->
    </el-card>
    <!-- 预览弹窗组件 -->
    <questions-preview :dialogVisible="dialogVisible" @close="dialogVisible = false" :questionItem="questionItem" />
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions'
import questionsPreview from '@/module-hmmm/components/questions-preview.vue'
export default {
  // 注册组件
  components: {
    questionsPreview
  },
  data () {
    return {
      // 题库数据
      questionItem: {},
      // 弹窗默认隐藏
      dialogVisible: false,
      // 查询信息 form
      query: { pagesize: 20, page: 1, keyword: '' },
      // 查询总条目
      count: 0,
      // 表单数据
      data: []
    }
  },
  computed: {
    // 警示框
    cTitle () {
      return '数据一共' + this.count + '条'
    }
  },
  methods: {
    // 页码变化
    handleSizeChange (val) {
      this.query.pagesize = val
      this.getRandoms()
    },
    // 当前页变化
    handleCurrentChange (val) {
      this.query.page = val
      this.getRandoms()
    },
    // },
    // 获取题组
    async getRandoms () {
      const { data } = await randoms(this.query)
      this.data = data.items
      this.count = data.counts
    },
    // 清除按钮
    clear () {
      this.query.keyword = ''
    },
    // 搜索按钮
    onSearch () {
      this.query.page = 1
      this.getRandoms()
    },
    // 删除题组
    async remove (item) {
      const res = await this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (res === 'confirm') {
        const data = await removeRandoms(item)
        console.log(data)
        this.getRandoms()
        this.$message.success('删除成功！')
      }
    },
    // 预览
    async preview (id) {
      try {
        const { data } = await detail({ id })
        this.questionItem = data
        this.dialogVisible = true
      } catch (error) { // 错误处理
        this.$message.info('数据未找到')
        return error
      }
    }
  },
  // 过滤器
  filters: {
    fMapQuestionType (value) {
      if (value === '1') {
        return '单选'
      } else if (value === '2') {
        return '多选'
      } else {
        return '简答'
      }
    }
  },
  // 加载页面铺设数据
  created () {
    this.getRandoms()
  }
}
</script>

<style scoped >
.number {
  color: #09f;
  cursor: pointer;
  line-height: 10px;
}
</style>
